<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->

        <tree-tool :node-data="companyInfo" :is-company="true" @showDialog="showDialog" />

        <!-- 分割线 -->
        <hr>
        <!-- 各部门 -->
        <el-tree :data="depts" default-expand-all="">
          <template v-slot="scoped">
            <tree-tool
              :node-data="scoped.data"
              :is-company="false"
              @showDialog="showDialog"
              @editDept="editDept"
              @onload="loadPage"
            />
          </template>
        </el-tree>
      </el-card>
      <add-dept
        ref="addDept"
        :is-show-dialog="isShowDialog"
        :node-data="nodeData"
        @reload="loadPage"
        @closeDialog="closeDialog"
      />
    </div>
  </div>
</template>

<script>
import TreeTool from './components/tree_tool.vue'
import addDept from './components/add-dept.vue'
import { getDepartmentsAPI } from '@/api/department'
import { listToTree } from '@/utils'
export default {
  components: {
    TreeTool,
    addDept
  },
  data() {
    return {
      companyInfo: {
        name: '💼传智播客黑马程序猿',
        manager: '负责人',
        // 由于后面添加部门的时候, 顶级部门跟在公司后面pid都是''空字符串
        // 这里给个 id 方便后面读取
        id: ''
      },
      depts: [],
      isShowDialog: false,
      // 临时储存被点击的对象
      nodeData: {}
    }
  },
  created() {
    this.loadPage()
  },
  methods: {
    async loadPage() {
      const { depts } = await getDepartmentsAPI()
      this.depts = listToTree(depts, '')
    },
    showDialog(nodeData) {
      this.isShowDialog = true
      this.nodeData = nodeData
    },
    closeDialog() {
      this.isShowDialog = false
    },
    editDept(nodeData) {
      this.nodeData = nodeData
      this.$refs.addDept.getDepartDetail(nodeData.id)
      this.isShowDialog = true
    }
  }
}
</script>

  <style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;

}
  </style>
